package com.google.code.garbagecan.springbatchstudy.message;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

public class Test {
	public static void main(String[] args) throws Exception {
		test1();
		test2();
	}
	
	public static void test1() throws Exception {
		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("/com/google/code/garbagecan/springbatchstudy/message/spring.xml");
		SimpleJobLauncher launcher = new SimpleJobLauncher();
		launcher.setJobRepository((JobRepository) c.getBean("jobRepository"));
		launcher.setTaskExecutor(new SimpleAsyncTaskExecutor());
		JobExecution jobExecution = launcher.run((Job) c.getBean("messageJob1"), new JobParameters());
		
		System.out.println(jobExecution); 
		System.out.println(jobExecution.getJobInstance()); 
		System.out.println(jobExecution.getStepExecutions());
		
		while (true) {
			if (jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) {
				break;
			}
			Thread.sleep(1000);
		}
	}
	
	public static void test2() throws Exception {
		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("/com/google/code/garbagecan/springbatchstudy/message/spring.xml");
		SimpleJobLauncher launcher = new SimpleJobLauncher();
		launcher.setJobRepository((JobRepository) c.getBean("jobRepository"));
		launcher.setTaskExecutor(new SimpleAsyncTaskExecutor());
		
		JobExecution jobExecution = null;
		JobParameters jobParameters = null;
		Job job = null;
		
		job = (Job) c.getBean("messageJob2");
		jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
		jobExecution = launcher.run(job, jobParameters);
		while (true) {
			if (jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) {
				break;
			}
			Thread.sleep(1000);
		}
		
		job = (Job) c.getBean("messageJob2");
		jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
		jobExecution = launcher.run(job, jobParameters);
		while (true) {
			if (jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) {
				break;
			}
			Thread.sleep(1000);
		}
	}
}
